package dbPhase.hypeerweb;

import org.junit.Test;

import junit.framework.TestCase;



public class AddNodeTest extends TestCase {
	public static HyPeerWeb hypeerweb = HyPeerWeb.getSingleton();
	@Test
	public void testAddNode() {
		hypeerweb.clear();
		boolean error = false;
		int errorCount = 0;

		Node node0 = new Node(0);
		hypeerweb.addToHyPeerWeb(node0, null);

		System.err.println("Size is empty, added node0");

		if (node0.webId.getValue() != 0) {
			System.err.println("Node 0: Webid Wrong: " + node0.webId.getValue()); error = true; errorCount++;
			}
		if (node0.fold.webId.getValue() != 0) {
			System.err.println("Node 0: Fold Wrong, should be 0: " + node0.fold.webId.getValue()); error = true; errorCount++;
			}
		if (node0.neighbors.size() != 0) {
			System.err.println("Node 0: neighborssize Wrong: " + node0.neighbors.size()); error = true; errorCount++;
			}
		if (node0.downPointers.size() != 0) {
			System.err.println("Node 0: downPointerssize Wrong: " + node0.downPointers.size()); error = true; errorCount++;
			}
		if (node0.upPointers.size() != 0) {
			System.err.println("Node 0: upPointersize Wrong: " + node0.upPointers.size()); error = true; errorCount++;
			}
		if (node0.nodeState != HypercubeCapState.getSingleton()) {
			System.err.println("Node 0: nodeState Wrong, should be Cap: " + node0.nodeState.toString()); error = true; errorCount++;
			}

		Node node1 = new Node(0);
		hypeerweb.addToHyPeerWeb(node1, node0);

		System.err.println("Size is 1, add node1");

		if (node0.webId.getValue() != 0) {
			System.err.println("Node 0: Webid Wrong: " + node0.webId.getValue()); error = true; errorCount++;
			}
		if (node0.fold.webId.getValue() != 1) {
			System.err.println("Node 0: Fold Wrong, should be 1: " + node0.fold.webId.getValue()); error = true; errorCount++;
			}
		if (node0.neighbors.size() != 1) {
			System.err.println("Node 0: neighborssize Wrong: " + node0.neighbors.size()); error = true; errorCount++;
			}
		if (node0.downPointers.size() != 0) {
			System.err.println("Node 0: downPointerssize Wrong: " + node0.downPointers.size()); error = true; errorCount++;
			}
		if (node0.upPointers.size() != 0) {
			System.err.println("Node 0: upPointersize Wrong: " + node0.upPointers.size()); error = true; errorCount++;
			}
		if (node0.nodeState != StandardNodeState.getSingleton()) {
			System.err.println("Node 0: nodeState Wrong, should be Standard: " + node0.nodeState.toString()); error = true; errorCount++;
			}

		if (node1.webId.getValue() != 1) {
			System.err.println("Node 1: Webid Wrong: " + node1.webId.getValue()); error = true; errorCount++;
			}
		if (node1.fold.webId.getValue() != 0) {
			System.err.println("Node 1: Fold Wrong, should be 0: " + node1.fold.webId.getValue()); error = true; errorCount++;
			}
		if (node1.neighbors.size() != 1) {
			System.err.println("Node 1: neighborssize Wrong: " + node1.neighbors.size()); error = true; errorCount++;
			}
		if (node1.downPointers.size() != 0) {
			System.err.println("Node 1: downPointerssize Wrong: " + node1.downPointers.size()); error = true; errorCount++;
			}
		if (node1.upPointers.size() != 0) {
			System.err.println("Node 1: upPointersize Wrong: " + node1.upPointers.size()); error = true; errorCount++;
			}
		if (node1.nodeState != HypercubeCapState.getSingleton()) {
			System.err.println("Node 1: nodeState Wrong, should be Cap: " + node1.nodeState.toString()); error = true; errorCount++;
			}

		Node node2 = new Node(0);
		hypeerweb.addToHyPeerWeb(node2, node0);
		System.err.println("Size is 2, added node2");



		if (node2.downPointers.size() != 1) {
			System.err.println("Node 2: downPointers size Wrong, should be 1: " + node2.downPointers.size()); error = true; errorCount++;
			}

		if (node0.nodeState != StandardNodeState.getSingleton()) {
			System.err.println("Node 0: nodeState Wrong, should be standard: " + node0.nodeState.toString()); error = true; errorCount++;
			}
		if (node1.nodeState != UpPointingNodeState.getSingleton()) {
			System.err.println("Node 1: nodeState Wrong, should be UpPointingNodeState: " +
								node1.nodeState.toString()); error = true; errorCount++;
			}
		if (node2.nodeState != TerminalNodeState.getSingleton()) {
			System.err.println("Node 1: nodeState Wrong, should be TerminalNodeState: " +
								node2.nodeState.toString()); error = true; errorCount++;
			}

		Node node3 = new Node(0);
		hypeerweb.addToHyPeerWeb(node3, node0);
		System.err.println("Size is 3, added node3");





		if (hypeerweb.size() != 4) {
			System.err.println("Hypeerwebsize wrong, should be 4: " + hypeerweb.size()); error = true; errorCount++;
			}
		if (node0.downPointers.size() != 0) {
			System.err.println("Node 0: downPointerssize Wrong, should be 0: " + node0.downPointers.size()); error = true; errorCount++;
			}
		if (node1.downPointers.size() != 0) {
			System.err.println("Node 1: downPointerssize Wrong, should be 0: " + node1.downPointers.size()); error = true; errorCount++;
			}
		if (node2.downPointers.size() != 0) {
			System.err.println("Node 2: downPointerssize Wrong, should be 0: " + node2.downPointers.size()); error = true; errorCount++;
			}
		if (node3.downPointers.size() != 0) {
			System.err.println("Node 3: downPointerssize Wrong, should be 0: " + node3.downPointers.size()); error = true; errorCount++;
			}

		if (node0.upPointers.size() != 0) {
			System.err.println("Node 0: upPointers size Wrong, should be 0: " + node0.upPointers.size()); error = true; errorCount++;
			}
		if (node1.upPointers.size() != 0) {
			System.err.println("Node 1: upPointers size Wrong, should be 0: " + node1.upPointers.size()); error = true; errorCount++;
			}
		if (node2.upPointers.size() != 0) {
			System.err.println("Node 2: upPointers size Wrong, should be 0: " + node2.upPointers.size()); error = true; errorCount++;
			}
		if (node3.upPointers.size() != 0) {
			System.err.println("Node 3: upPointers size Wrong, should be 0: " + node3.upPointers.size()); error = true; errorCount++;
			}

		Node node4 = new Node(0);
		hypeerweb.addToHyPeerWeb(node4, node0);
		System.err.println("Size is 4, added node4");
		Node node5 = new Node(0);
		hypeerweb.addToHyPeerWeb(node5, node0);
		System.err.println("Size is 5, added node5");

		printoutstates();

		Node node6 = new Node(0);
		hypeerweb.addToHyPeerWeb(node6, node0);
		System.err.println("Size is 6, added node6");

		printoutstates();
		Node node7 = new Node(0);
		hypeerweb.addToHyPeerWeb(node7, node0);
		System.err.println("Size is 7, added node7");

		printoutstates();


		if (hypeerweb.size() != 8) {
			System.err.println("Hypeerwebsize wrong, should be 8: " + hypeerweb.size()); error = true; errorCount++;
			}

		if (node0.webId.getValue() != 0) {
			System.err.println("Node 0: Webid Wrong: " + node0.webId.getValue()); error = true; errorCount++;
			}
		if (node1.webId.getValue() != 1) {
			System.err.println("Node 1: Webid Wrong: " + node1.webId.getValue()); error = true; errorCount++;
			}
		if (node2.webId.getValue() != 2) {
			System.err.println("Node 2: Webid Wrong: " + node2.webId.getValue()); error = true; errorCount++;
			}
		if (node3.webId.getValue() != 3) {
			System.err.println("Node 3: Webid Wrong: " + node3.webId.getValue()); error = true; errorCount++;
			}
		if (node4.webId.getValue() != 4) {
			System.err.println("Node 4: Webid Wrong: " + node4.webId.getValue()); error = true; errorCount++;
			}
		if (node5.webId.getValue() != 5) {
			System.err.println("Node 5: Webid Wrong: " + node5.webId.getValue()); error = true; errorCount++;
			}
		if (node6.webId.getValue() != 6) {
			System.err.println("Node 6: Webid Wrong: " + node6.webId.getValue()); error = true; errorCount++;
			}
		if (node7.webId.getValue() != 7) {
			System.err.println("Node 7: Webid Wrong: " + node7.webId.getValue()); error = true; errorCount++;
			}

		assertFalse(error);

	//	assertTrue(node5.downPointers.contains(node7));
	//	assertFalse(node5.downPointers.contains(node5));
	}
	private void printoutstates() {
		System.err.println("webSize: " + hypeerweb.size());
		for (int a = 0; a < hypeerweb.size(); a++) {
			Node h = hypeerweb.getNode(a);
			System.err.println(h.getWebId().getValue() + " is " + h.nodeState);
		}

	}

}
